﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ession.OCR
{
    /// <summary>
    ///     任务链,一个接一个完成
    /// </summary>
    /// <remarks>
    ///     
    /// </remarks>
    public class TaskChain
    {
        public List<Task> Tasks = new List<Task>();
        public event Action<List<Task>> ExecuteComplete;
        public void Execute()
        {
            ThreadPool.QueueUserWorkItem(o =>
            {
                for (int i = 0; i < Tasks.Count; i++)
                {
                    if (i < Tasks.Count - 1)
                    {
                        int i1 = i;
                        Tasks[i].ContinueWith(t => Tasks[i1 + 1].Start());
                    }
                }
                if (Tasks.Count > 0)
                {
                    Tasks[0].Start();
                }
                Task.WaitAll(Tasks.ToArray());
                if (ExecuteComplete != null)
                {
                    ExecuteComplete(Tasks);
                }
            });

        }
    }
}
